package com.wdzg.member.generate.freemaker.generator.metadata;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.commons.lang.StringUtils;

public class JDBCUtil {
	private static String driverClass = "oracle.jdbc.driver.OracleDriver";
	private static String dbUrl = "jdbc:oracle:thin:@10.20.1.91:1521:orcl";
	private static String username = "mrbt_oa";
	private static String password = "mrbt_oa";
	private static String tablename = "STORE_MEMBER_BANNER";
	private static String owner = "MRBT_OA";
	private static Connection con = null;	
	
	/**
	 * 获取数据库连接
	 * 	如果没有则创建一个
	 * @return
	 */
	public static Connection getConnection(){
		try {
			if (con == null) {
				Class.forName(driverClass);
				con = DriverManager.getConnection(dbUrl, username, password);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	    
		return con;
	}
	
	/**
	 * 关闭这个数据库连接
	 */
	public static void disConnection(){
		try {
			if (con!=null && !con.isClosed()) {
				con.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 替换掉下划线并让紧跟它后面的字母大写
	 * @return
	 */
	public static String replaceUnderlineAndfirstToUpper(String stcStr){
		StringBuffer sb = new StringBuffer();
		sb.append(stcStr);
		
		int count = sb.indexOf("_");
		while(count != 0){
			int num = sb.indexOf("_",count);
			count = num+1;
			if (num != -1) {
				char ss = sb.charAt(count);
				char ia = (char) (ss - 32);
				sb.replace(count,count+1,ia+"");
				//System.out.println(sb.toString());
			}
		}
		String ss = sb.toString().replaceAll("_","");
		
		return ss;
	}
	
	/**
	 * 驼峰标示
	 * @param srcStr
	 * @return
	 */
	public static String humpMarking(String srcStr){
		return humpMarking(srcStr, "_", "");
	}
	
	/**
	 * 替换字符串并让它的下一个字母为大写
	 * @param srcStr
	 * @param org
	 * @param ob
	 * @return
	 */
	public static String humpMarking(String src,String org,String ob) {
		if (StringUtils.isBlank(src) || StringUtils.isBlank(org)) {
			return src;
		}
		
		String newString = "";
		
		String str[] = src.split(org);
		for(String s : str){
			newString += fcToUpper(s);
		}
		
		return newString;
	}
	
	/**
	 * 首字母大写，其他字母小写
	 *
	 * @param srcStr
	 * @return
	 */
	public static String fcToUpper(String srcStr) {
		 return srcStr.substring(0, 1).toUpperCase() + srcStr.substring(1).toLowerCase();
	}

	
	public static String getTablename() {
		return tablename;
	}

	public static void setTablename(String tablename) {
		JDBCUtil.tablename = tablename;
	}

	public static String getOwner() {
		return owner;
	}

	public static void setOwner(String owner) {
		JDBCUtil.owner = owner;
	}
}
